package com.xmy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.study.entity.PerStudentClassType;
import com.xmy.study.mapper.PerStudentClassTypeMapper;
import com.xmy.study.service.IPerStudentClassTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-06-25
 */
@Service
public class PerStudentClassTypeServiceImpl extends ServiceImpl<PerStudentClassTypeMapper, PerStudentClassType> implements IPerStudentClassTypeService {

    @Autowired
    PerStudentClassTypeMapper perStudentClassTypeMapper;

    @Override
    public String getGradeByStudentId(Integer yearPart, Integer quarterNum, Long studentId) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        wrapper.eq("subjects_id", 1);
        wrapper.eq("is_boutique", 1);
        wrapper.eq("reading_status", 1);
        wrapper.eq("year_part", yearPart);
        wrapper.eq("quarter_num", quarterNum);
        wrapper.groupBy("grade_id");
        wrapper.last("limit 1");
        PerStudentClassType perStudentClassType = perStudentClassTypeMapper.selectOne(wrapper);

        if (perStudentClassType != null) {
            wrapper.clear();

            wrapper.eq("grade_id", perStudentClassType.getGradeId());
            wrapper.eq("reading_status", 1);
            wrapper.eq("is_boutique", 1);
            List<PerStudentClassType> studentClassTypeList = perStudentClassTypeMapper.selectList(wrapper);

            StringBuffer sb = new StringBuffer();
            for (PerStudentClassType studentClassType : studentClassTypeList) {
                sb.append(studentClassType.getStudentId() + ",");
            }

            String result = sb.toString();
            if (result.endsWith(",")) {
                result = result.substring(0, result.length() - 1);
            }
            return result;
        }

        return null;
    }

    @Override
    public PerStudentClassType getPerStudentClassType(PerStudentClassType perStudentClassType) {
        return perStudentClassTypeMapper.getPerStudentClassType(perStudentClassType);
    }
}
